<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */
namespace MoChat\Plugin\TenantProduct\Action\Admin\Sku;

use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use MoChat\Framework\Action\AbstractAction;
use MoChat\Framework\Request\ValidateSceneTrait;
use MoChat\Plugin\AdminUser\Middleware\AdminAuthMiddleware;
use MoChat\Plugin\AdminUser\Middleware\PermissionMiddleware;
use MoChat\Plugin\TenantProduct\Logic\StoreLogic;

/**
 * 套餐管理- 创建提交.
 *
 * Class Store.
 * @Controller
 */
class Store extends AbstractAction
{
    use ValidateSceneTrait;

    /**
     * @Inject
     * @var StoreLogic
     */
    protected $storeLogic;

    /**
     * @Inject
     * @var StdoutLoggerInterface
     */
    private $logger;

    /**
     * @RequestMapping(path="/admin/tenantProduct/sku/store", methods="post")
     * @Middlewares({
     *     @Middleware(AdminAuthMiddleware::class),
     *     @Middleware(PermissionMiddleware::class)
     * })
     * @return array 返回数组
     */
    public function handle(): array
    {
        // 参数验证
        $this->validated($this->request->all());
        // 接收参数
        $params = [
            'spuId' => $this->request->input('spuId', 1),
            'attributes' => $this->request->input('attributes'),
            'stock' => $this->request->input('stock', 999999),
            'marketPrice' => $this->request->input('marketPrice'),
            'price' => $this->request->input('price'),
            'auditStatus' => $this->request->input('auditStatus'),
        ];

        return $this->storeLogic->handle($params);
    }

    /**
     * 验证规则.
     *
     * @return array 响应数据
     */
    protected function rules(): array
    {
        return [
            'attributes' => 'required | array',
            'marketPrice' => 'required',
            'price' => 'required',
            'auditStatus' => 'required | integer | in:1,2, | bail',
        ];
    }

    /**
     * 验证错误提示.
     * @return array 响应数据
     */
    protected function messages(): array
    {
        return [
            'attributes.required' => '销售属性 必填',
            'attributes.array' => '销售属性 必需为二维数组',
            'marketPrice.required' => '套餐原价 必填',
            'price.required' => '套餐现价 必填',
            'auditStatus.required' => '审核状态 必填',
            'auditStatus.integer' => '审核状态 必需为整数',
            'auditStatus.in' => '审核状态 值必须在列表内：[1,2]',
        ];
    }
}
